package mercurius.fitness.client.ui;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.util.Entry;
import mercurius.fitness.client.util.HTTPUtil;
import mercurius.fitness.client.util.Logger;
import net.pleso.framework.client.ui.windows.Slider;
import net.pleso.framework.client.ui.windows.Window;


public class RemoteHTMLFrame extends Window {
	
	public RemoteHTMLFrame(String caption,Entry[] entries) {
		super(null);
		init(caption,entries);
		
	}
	
	private final SimplePanel p = new SimplePanel();
	private void init(final String caption,final Entry[] entries) {
		this.setCaption(caption);
		
		
		final HTML html = new HTML();
		
		p.add(new HTML("Loading ..."));
		
		HTTPUtil.doPost(Fitness.get().getURL(),entries,new RequestCallback() {

			public void onResponseReceived(Request request, Response response) {
				String str = response.getText();
				
				if(str.length() > 0) {
					 int height = com.google.gwt.user.client.Window.getClientHeight() -75;
					str = str.replaceAll("HEIGHT='.*'","HEIGHT='"+ height+"px'");
					
					html.setHTML(str);
				} else {
					html.setHTML("No data returned");
				}
				Logger.debug("Returned: "+ response.getText());
				
				setupHTMLFrame(html,entries);
			}

			public void onError(Request request, Throwable exception) {
				p.clear();
				p.add(new HTML("Error occured, check logs"));
				Logger.error("Error occured, check logs.",exception);
			}
		});

		
		this.initWidget(p);
		
//		
		
	}
	
	private void setupHTMLFrame(final HTML html,final Entry[] entries) {
		final int height = com.google.gwt.user.client.Window.getClientHeight() -75;
		
		html.setSize("100%",height + "px"); 
		
		final HTML link = new HTML("<a href='javascript:;'>Open in new window</a>");
		link.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Fitness.open(Fitness.get().getURL()+"?"+HTTPUtil.buildQueryString(entries)+"&fulldisplay=true");
			}
		});
		
		 
		final VerticalPanel v = new VerticalPanel();
		v.add(link);
		v.add(html);
		v.setWidth("100%");
		p.clear();
		p.add(v);
		
		// Add a listener for browser resize events.
			com.google.gwt.user.client.Window.addWindowResizeListener(new WindowResizeListener() {
	            public void onWindowResized(int width, int height)
	            {
	                height = height - 75;
	                
	              //  String str = html.getHTML().replaceAll("HEIGHT='.*'","HEIGHT='"+ height+"px'");
	             //   html .setHTML(str);
	                html.setHeight(height + "px");   
	            }
	        }); 
	}
	
	public RemoteHTMLFrame(Slider parentSlider,String caption,Entry[] entries) {
		super(parentSlider);
		init(caption,entries);
	}

}
